package com.jy.controller;

import com.jy.common.Base64Image;
import com.jy.common.UUIDUtils;
import com.jy.model.Account;
import com.jy.model.Product_release;
import com.jy.model.User;
import com.jy.model.WithdrawalRecord;
import com.jy.service.Product_releaseService;
import com.jy.service.UserInfoService;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.List;
import java.util.Properties;

@Controller
@RequestMapping("/productRelease")
public class Product_releaseController {
	@Resource
	private Product_releaseService productService;
	@Autowired
	UserInfoService userInfoService;

	@RequestMapping("/getInvest")
	public @ResponseBody String getInvest(Integer start, Integer length, String product_name, String syl,String [] ids) {
		List<Product_release> list = productService.getInvestInfo(start, length, product_name, syl,ids);
		if(ids!=null){
			for(Product_release pro:list){
				/*if(pro.getProduct_investment_amount()==0)
					pro.setProduct_investment_amount(pro.getMin_money());*/
			}
		}
		// int total = productService.getInvestCount(product_name, syl);
		// JSONObject ab=new JSONObject();//
		
		 //* ab.put("list", list); ab.put("count", total);
		
		JSONArray jsons = new JSONArray();
		return JSONArray.fromObject(list).toString();
		/*//List<Department>list=userInfoService.getDepartment("大区","3");
        JSONObject jb=null;
        JSONArray jsons=new JSONArray();
        for(int i=0;i<10;i++){
            jb=new JSONObject();
            jb.put("id", "111");
            jb.put("name","222");
            jsons.add(jb);
        }
        return jsons.toString();*/
	}
	@RequestMapping("/saveInvest")
	public @ResponseBody
	JSON saveInvest(String json, String userid, String username)
			throws ParseException {
		JSONObject jo=new JSONObject();
		Account a=new Account();
		JSONArray ja=JSONArray.fromObject(json);
		@SuppressWarnings("unchecked")
		List<Product_release> li=(List<Product_release>)JSONArray.toCollection(ja,Product_release.class);
		int truck=0;
		for(Product_release p:li){
			double ins = p.getProduct_investment_amount();

			/**
			 * 根据ID查询开始与结束时间
			 */
			double yield = p.getYield();
			double minMoney = p.getMin_money();// 最小金额
			System.out.println("minMoney======" + minMoney);
			String time = p.getEnd_time();// 结束时间
			System.out.println("time=====" + time);
			long cha = 0;
			if (p != null) {
				p.getStart_time();
				p.getEnd_time();
				/**
				 * 计算时间之间的天数差
				 */
				java.text.SimpleDateFormat format = new java.text.SimpleDateFormat(
						"yyyy-MM-dd");
				java.util.Date beginDate;
				java.util.Date endDate;
				beginDate = format.parse(p.getStart_time());
				endDate = format.parse(p.getEnd_time());
				cha = (endDate.getTime() - beginDate.getTime())
						/ (24 * 60 * 60 * 1000);
			}
			// 每天收益额
			double daySY = ins * yield * cha / 360;
			a.setId(UUIDUtils.getUUID());
			a.setProduct_id(p.getId());
			a.setInvestment_amount(ins);// 投资金额
			a.setInvestor(username);
			a.setUser_id(userid);
			a.setSyl(yield);// 收益率
			a.setTzsy(daySY);// //每天收益额
			a.setWithdraw_time(time);
			System.out.println("a.getInvestment_amount()------"+a.getInvestment_amount());
			if (a.getInvestment_amount() >= minMoney) {
				System.out.println("a.getInvestment_amount()------"+a.getInvestment_amount());
				truck += productService.saveInvest(a);
			} else {
				return (JSON) jo.put("suc", "0");
			}
		}
		return (JSON) jo.put("suc", truck+"");
	}

	/**
	 * 我的产品查询
	 * @param state
	 * @param size
	 * @param token
	 * @return
	 */
	@RequestMapping("/getProduct")
	public @ResponseBody
	List<Product_release> getProduct(Integer state, Integer size, String token) {
		List<Product_release> list = productService.getProductInfo(state,size,token);
		return list;
	}

	/**
	 * 单个我的产品详细信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/getsingProduct")
	public @ResponseBody
	Account getsingProduct(String id) {
		Account list = productService.getsingProduct(id);
		return list;
	}

	/**
	 * 我的荷包查询
	 * @param state
	 * @param size
	 * @param token
	 * @return
	 */
	@RequestMapping("/getPouch")
	public @ResponseBody List<Account> getPouch(Integer state, Integer size, String token) {
		List<Account> list = productService.getPouchInfo(state,size,token);
		return list;
	}

	/**
	 * 我的荷包单个详细信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/getsingPouch")
	public @ResponseBody
	Account getsingPouch(String id) {
		Account list = productService.getsingPouch(id);
		return list;
	}
	/**
	 * 提现申请
	 * @param id
	 * @return
	 */
	@RequestMapping("/updateWith")
	public @ResponseBody
	boolean updateWith(String id,String withdraw_time) {
		int list = productService.updateWith(id,withdraw_time);
		if(list>0){
			return true;
		}else{
			return false;
		}}
	//我的产品提现申请
	@RequestMapping( "/updateMyProductTxian")
	public @ResponseBody
	boolean updateMyProductTxian(Account account,String withdraw_time,HttpSession session) {
		account.setApply_time(withdraw_time);
		int edit = productService.updateTxian(account);
		if (edit > 0) {
			return true;
		}else{
			return false;
		}

	}

	//提现记录查询
	@RequestMapping("/getTxjlInfo")
	public @ResponseBody
	List<WithdrawalRecord> getTxjlInfo(Integer state, Integer size, String token) {
		List<WithdrawalRecord> list = productService.getTxjlInfo(state,size,token);
		return list;
	}

	@RequestMapping("/getsingRecord")
	public @ResponseBody
	WithdrawalRecord getsingRecord(String id) {
		WithdrawalRecord withdrawalRecord = productService.getsingRecord(id);
		return withdrawalRecord;
	}

	//提现记录查询
	@RequestMapping("/updatetouxiang")
	public @ResponseBody
	boolean updatetouxiang(String id,String imgs,String imgsname) {
		String name=toimages(imgs,imgsname);
		int a= productService.updateTouxiang(id,name);
		if(a>0){
			return true;
		}else{
			return false;
		}
	}
	public String toimages(String imgs,String imgsname){
		/*String imgs=user.getImgs();
		String imgs2=user.getImgs2();
		String imgs3=user.getImgs3();*/
		if(!"".equals(imgs)&&imgs!=null){
			imgs=imgs.substring(imgs.indexOf(",")+1);
		}

		String	filepath= Base64Image.GenerateImage(imgs,createPath(imgsname));
		//user.setCard_information(filepath);
		return filepath;
	}
	public  String createPath(String imagName){
		Properties prop =  new  Properties();
		InputStream ins =LoginController.class.getResourceAsStream("/filepath.properties");
		try {
			prop.load(ins);
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		String savePath=prop.getProperty("filepath");
		File filefolder=new File(savePath);
		if(!filefolder.isDirectory()){
			filefolder.mkdirs();
		}
		String a=imagName.substring(0, imagName.indexOf("."));
		String b=imagName.substring(imagName.indexOf("."));
		long seconds=System.currentTimeMillis();
		String path=savePath+"/"+a+"-"+seconds+b;
		return path;
	}

	//投资记录查询
	@RequestMapping("/getInvestRecorde")
	public @ResponseBody
	List<WithdrawalRecord> getInvestRecorde(Integer state, Integer size, String token) {
		List<WithdrawalRecord> list = productService.tzRecordeInfo(state,size,token);
		return list;
	}

	@RequestMapping("/getsingTzRecord")
	public @ResponseBody
	WithdrawalRecord getsingTzRecord(String id) {
		WithdrawalRecord withdrawalRecord = productService.getsingTzRecord(id);
		return withdrawalRecord;
	}

}
